<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <th:block th:include="include :: header('排班管理列表')"/>
    <style>
        .layui-card-body .layui-form {
            margin: auto;
        }
        .layui-form-item {
            margin-bottom: 0px;
        }
    </style>
</head>
<body class="layuimini-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form id="formId" class="layui-form layui-form-pane">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">机构</label>
                    <div class="layui-input-inline">
                        <input type="hidden" id="deptId">
                        <ul data-id="0" id="selectDept" name="deptName"></ul>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="username" placeholder="请输入名称"
                               class="layui-input"  lay-required/>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">年份</label>
                    <div class="layui-input-inline">
                        <input type="text" id="year" name="day" placeholder="请输入年份"
                               class="layui-input"  lay-required/>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-md" lay-submit lay-filter="sche-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="layui-btn layui-btn-md layui-btn-primary" id="formreset">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
        <table class="layui-table" id="sche-table" lay-filter="sche-table">

        </table>
        <script type="text/html" id="sche-toolbar">
            <button sec:authorize="hasPermission('/kq/sche/add','kq:sche:add')"
                    class="layui-btn layui-btn-primary layui-btn-md" lay-event="add">
                <i class="layui-icon layui-icon-add-1"></i>
                新增
            </button>
            <button sec:authorize="hasPermission('/kq/sche/remove','kq:sche:del')"
                    class="layui-btn layui-btn-danger layui-btn-md" lay-event="batchRemove">
                <i class="layui-icon layui-icon-delete"></i>
                删除
            </button>
        </script>
        <script type="text/html" id="sche-bar">
            <button sec:authorize="hasPermission('/kq/sche/edit','kq:sche:edit')"
                    class="layui-btn layui-btn-primary layui-btn-sm" lay-event="edit"><i
                    class="layui-icon layui-icon-edit"></i>
            </button>
            <button sec:authorize="hasPermission('/kq/sche/remove','kq:sche:del')"
                    class="layui-btn layui-btn-danger layui-btn-sm" lay-event="remove"><i
                    class="layui-icon layui-icon-delete"></i>
            </button>
        </script>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script>
    var _form;
    let table;
    layui.use(['table', 'form', 'jquery', 'dtree', 'laydate'], function () {
        table = layui.table;
        let form = layui.form;
        _form = form;
        let $ = layui.jquery;
        let dtree = layui.dtree;
        let laydate = layui.laydate;

        let prefix = pName + "/kq/sche/";
        deptUtil.initDeptSelectTree("selectDept", "deptId", "deptName", 1)
        laydate.render({
            elem: '#year',
            trigger: 'click',
            type: 'year', //设置日期类型
            format: 'yyyy-MM', //设置日期格式
            // showBottom: false, //关闭底部框 去掉取消、确定、清空按钮
            done: function (value, date) {
                console.log(value)
            }

        });

        let cols = [
            [
                {type: 'checkbox',
                    fixed: true},
                {
                    field: 'username',
                    title: '姓名',
                    width: 80,
                    fixed: true
                },
                {
                    field: 'userSn',
                    title: '工号',
                    width: 80
                },
                {
                    field: 'scheName',
                    title: '班制名称',
                    width: 80
                },
                {
                    field: 'scheStage',
                    title: '班段',
                    width: 80
                },
                {
                    field: 'day',
                    title: '日期',
                    width: 80
                },
                {
                    field: 'd1',
                    title: '1日',
                    width: 80
                },
                {
                    field: 'd2',
                    title: '2日',
                    width: 80
                },
                {
                    field: 'd3',
                    title: '3日',
                    width: 80
                },
                {
                    field: 'd4',
                    title: '4日',
                    width: 80
                },
                {
                    field: 'd5',
                    title: '5日',
                    width: 80
                },
                {
                    field: 'd6',
                    title: '6日',
                    width: 80
                },
                {
                    field: 'd7',
                    title: '7日',
                    width: 80
                },
                {
                    field: 'd8',
                    title: '8日',
                    width: 80
                },
                {
                    field: 'd9',
                    title: '9日',
                    width: 80
                },
                {
                    field: 'd10',
                    title: '10日',
                    width: 80
                },
                {
                    field: 'd11',
                    title: '11日',
                    width: 80
                },
                {
                    field: 'd12',
                    title: '12日',
                    width: 80
                },
                {
                    field: 'd13',
                    title: '13日',
                    width: 80
                },
                {
                    field: 'd14',
                    title: '14日',
                    width: 80
                },
                {
                    field: 'd15',
                    title: '15日',
                    width: 80
                },
                {
                    field: 'd16',
                    title: '16日',
                    width: 80
                },
                {
                    field: 'd17',
                    title: '17日',
                    width: 80
                },
                {
                    field: 'd18',
                    title: '18日',
                    width: 80
                },
                {
                    field: 'd19',
                    title: '19日',
                    width: 80
                },
                {
                    field: 'd20',
                    title: '20日',
                    width: 80
                },
                {
                    field: 'd21',
                    title: '21日',
                    width: 80
                },
                {
                    field: 'd22',
                    title: '22日',
                    width: 80
                },
                {
                    field: 'd23',
                    title: '23日',
                    width: 80
                },
                {
                    field: 'd24',
                    title: '24日',
                    width: 80
                },
                {
                    field: 'd25',
                    title: '25日',
                    width: 80
                },
                {
                    field: 'd26',
                    title: '26日',
                    width: 80
                },
                {
                    field: 'd27',
                    title: '27日',
                    width: 80
                },
                {
                    field: 'd28',
                    title: '28日',
                    width: 80
                },
                {
                    field: 'd29',
                    title: '29日',
                    width: 80
                },
                {
                    field: 'd30',
                    title: '30日',
                    width: 80
                },
                {
                    field: 'd31',
                    title: '31日',
                    width: 80
                },
                {title: '操作', toolbar: '#sche-bar', align: 'center', width: 130, fixed: 'right'}
            ]
        ]

        table.render({
            elem: '#sche-table',
            url: prefix + 'data',
            page: true,
            cols: cols,
            skin: 'line',
            escape: true,
            scrollX: true, // 启用横向滚动
            toolbar: '#sche-toolbar',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
            ,height: 'full-100'
        });

        table.on('checkbox(sche-table)', function(obj){

        });
        table.on('cell(sche-table)', function (obj) {
            layer.msg('单元格点击事件触发，列：' + obj.field + '，值：' + obj.value);
        });
        table.on('tool(sche-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(sche-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(sche-query)', function (data) {
            table.reload('sche-table', {where: data.field})
            return false;
        });

        form.on('select(scheType)', function (data) {
            let scheType = $('#scheType').val();
            let deptId = $('#deptId').val();

            $('#scheName').empty();
            form.render("select", "scheName");

            if(scheType == '' || deptId == ''){
                return;
            }
            $.ajax({
                url: prefix + "scheNames",
                dataType: 'json',
                data: {"scheType": scheType, "unitId": deptId},
                type: 'get',
                success: function (result) {
                    if (result.code == 1) {
                        result.data.forEach(function (n) {
                            $('#scheName').append(new Option(n, n));
                        })
                        form.render("select", "scheName");
                    } else {
                            layer.alert(result.msg, {icon: 2});
                        }
                }
            })
        });

        window.add = function () {
            let layerIndex = layer.open({
                type: 2,
                title: '新增排班管理',
                shade: 0.1,
                maxmin : true,
                area: ['99%', '99%'],
                content: prefix + 'add'
            });
            $(window).on("resize", function () {
                layer.full(layerIndex);
            });
            return false;
        }

        window.edit = function (obj) {
            let layerIndex = layer.open({
                type: 2,
                title: '修改排班管理',
                shade: 0.1,
                maxmin : true,
                area: ['99%', '99%'],
                content: prefix + 'edit?id=' + obj.data['id']
            });
            $(window).on("resize", function () {
                layer.full(layerIndex);
            });
            return false;
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该排班管理', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 1) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.alert(result.msg, {icon: 2});
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].id + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定要删除这些排班管理', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "batchRemove",
                    dataType: 'json',
                    data: {"ids": ids},
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 1) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                table.reload('sche-table');
                            });
                        } else {
                            layer.alert(result.msg, {icon: 2});
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('sche-table', {where: param});
        }
        $('#formreset').click(function(){
            $('#formId').find('input').val("")

            $('#scheName').empty();
            _form.render("select", "scheName");
        })
    })
</script>
</body>
</html>